home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / onechild.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  79 lines

  1. #include "kiss.h"
  2.  
  3. void onechild (Stringstack org, int indes, int outdes,
  4.            int allow_from, int allow_to)
  5. {
  6.     char
  7.     fname [LINELEN];
  8.     Redirect
  9.     red;
  10.     FILE
  11.     *outf,
  12.     *inf;
  13.     Stringstack
  14.     s;
  15.  
  16.     /* expand any aliased commands */
  17.     s = expandalias (org);
  18.     
  19.     /* see if we're redirecting */
  20.     if ( (red = redirected (&s, fname)) )
  21.     {
  22.     /* try to open the file, see if we may */
  23.     switch (red)
  24.     {
  25.         case writeto:
  26.         if (! allow_to)
  27.         {
  28.             warning ("%s: not allowed to redirect to file",
  29.                  s.str [0]);
  30.             return;
  31.         }
  32.         if (! (outf = fopen (fname, "w")) )
  33.         {
  34.             warning ("cannot open redirect file \"%s\" for writing",
  35.                  fname);
  36.             return;
  37.         }
  38.         outdes = fileno (outf);
  39.         break;
  40.         case appendto:
  41.         if (! allow_to)
  42.         {
  43.             warning ("%s: not allowed to redirect to file",
  44.                  s.str [0]);
  45.             return;
  46.         }
  47.         if (! (outf = fopen (fname, "a")) &&
  48.             ! (outf = fopen (fname, "w")) )
  49.         {
  50.             warning ("cannot open redirect file \"%s\" for appending",
  51.                  fname);
  52.             return;
  53.         }
  54.         outdes = fileno (outf);
  55.         break;
  56.         case readfrom:
  57.         if (! allow_from)
  58.         {
  59.             warning ("%s: not allowed to redirect from file",
  60.                  s.str [0]);
  61.             return;
  62.         }
  63.         if (! (inf = fopen (fname, "r")))
  64.         {
  65.             warning ("cannot open redirect file \"%s\" for reading",
  66.                  fname);
  67.             return;
  68.         }
  69.         indes = fileno (inf);
  70.         break;
  71.         default:
  72.         warning ("internal error [1] in onechild");
  73.     }
  74.     }
  75.  
  76.     /* launch program */
  77.     launch (s, indes, outdes);
  78. }
  79.